import datetime
import random

from flask import current_app
from flask_script import Manager
from flask_migrate import MigrateCommand
from info import create_app

# 创建app
app = create_app("dev")

# 创建脚本管理器
mgr = Manager(app)

# 将迁移命令添加到脚本管理器中
mgr.add_command("mc", MigrateCommand)


# 添加一个生成超级管理员的命令  option(命令参数名,  dest="函数参数名")
@mgr.option("-name", "-n", dest="name")
@mgr.option("-password", "-p", dest="password")
def create_superuser(name, password):
    from info.models import User
    from info import db
    if not all([name, password]):
        print("参数不足")
        return

    # 生成一个用户模型
    user = User()
    user.mobile = name
    user.password = password
    user.nick_name = name
    # 设置is_admin属性为True
    user.is_admin = True
    # 保存到数据库
    try:
        db.session.add(user)
        db.session.commit()
    except BaseException as e:
        current_app.logger.error(e)

    print("超级管理员注册成功")


def add_test_users():
    from info import db
    from info.models import User
    users = []
    now = datetime.datetime.now()
    for num in range(0, 10000):
        try:
            user = User()
            user.nick_name = "%011d" % num
            user.mobile = "%011d" % num
            user.password_hash = "pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829"
            user.last_login = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            user.create_time = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    db.session.add_all(users)
    db.session.commit()
    print('OK')



if __name__ == '__main__':
    mgr.run()
    # add_test_users()